package com.dianxin.smartpark.service.attendance.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkAttendanceStatistics;
import com.dianxin.smartpark.dao.mapper.attendance.SmartParkAttendanceStatisticsMapper;
import com.dianxin.smartpark.dao.qo.attendance.SmartParkAttendanceStatisticsQO;
import com.dianxin.smartpark.service.attendance.SmartParkAttendanceStatisticsService;
import com.dianxin.smartpark.service.vo.attendance.SmartParkAttendanceRecordVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: smart-park
 * @description: 考勤统计
 * @author: chenyong
 * @create: 2021-09-01 19:40
 */
@Service
public class SmartParkAttendanceStatisticsServiceImpl implements SmartParkAttendanceStatisticsService {

    @Resource
    private SmartParkAttendanceStatisticsMapper smartParkAttendanceStatisticsMapper;

    /**
     * 考勤日报
     * @Description:  考勤日报
     * @Author: ChenYong
     * @Date: 2021/9/1 0001
     * @param smartParkAttendanceStatisticsQO:
     * @Return: List<SmartParkAttendanceStatistics>
     */
    @Override
    public CommonResponseBody<List<SmartParkAttendanceStatistics>> selectByMonth(SmartParkAttendanceStatisticsQO smartParkAttendanceStatisticsQO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SmartParkAttendanceStatistics smartParkAttendanceStatistics = new SmartParkAttendanceStatistics();
        if(StringUtils.isEmpty(smartParkAttendanceStatisticsQO.getStartTime()) && StringUtils.isEmpty(smartParkAttendanceStatisticsQO.getEndTime())){
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.set(Calendar.DAY_OF_MONTH,1);

            Calendar endCalendar = Calendar.getInstance();
            endCalendar.set(Calendar.DAY_OF_MONTH, 31);

            simpleDateFormat.format(endCalendar.getTime());
            smartParkAttendanceStatisticsQO.setStartTime(simpleDateFormat.format(startCalendar.getTime()));
            smartParkAttendanceStatisticsQO.setEndTime(simpleDateFormat.format(endCalendar.getTime()));
        }

        try {
            BeanUtil.conversion(smartParkAttendanceStatisticsQO,smartParkAttendanceStatistics);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (smartParkAttendanceStatisticsQO.getNeedPage()) {
            smartParkAttendanceStatistics.setOrderByClause("user_name desc limit " + (smartParkAttendanceStatisticsQO.getPageIndex() - 1) * smartParkAttendanceStatisticsQO.getPageSize() + "," + smartParkAttendanceStatisticsQO.getPageSize());
        } else {
            smartParkAttendanceStatistics.setOrderByClause("user_name asc");
        }
        List<SmartParkAttendanceStatistics> list = smartParkAttendanceStatisticsMapper.selectByMonth(smartParkAttendanceStatistics);
        List<SmartParkAttendanceStatistics> smartParkAttendanceStatisticsList = new ArrayList<>();
        for (SmartParkAttendanceStatistics statistics:list){
            if(Integer.valueOf(statistics.getClockInStatusName()) == 0){
                statistics.setClockInStatusName("正常");
            }else {
                statistics.setClockInStatusName("异常");
            }
            smartParkAttendanceStatisticsList.add(statistics);
        }
        long count = smartParkAttendanceStatisticsMapper.countByMonth(smartParkAttendanceStatistics);

        return ResponseBodyWrapperUtil
                .successListWrapper(smartParkAttendanceStatisticsList, count, smartParkAttendanceStatisticsQO, SmartParkAttendanceStatistics.class);

    }


    /**
     * 考勤月报
     * @Description:  考勤月报
     * @Author: ChenYong
     * @Date: 2021/9/1 0001
     * @param smartParkAttendanceStatisticsQO:
     * @Return: List<SmartParkAttendanceStatistics>
     */
    @Override
    public CommonResponseBody<List<SmartParkAttendanceStatistics>> selectByDay(SmartParkAttendanceStatisticsQO smartParkAttendanceStatisticsQO) {
        SmartParkAttendanceStatistics smartParkAttendanceStatistics = new SmartParkAttendanceStatistics();
        Date date = new Date();
        if(StringUtils.isEmpty(smartParkAttendanceStatisticsQO.getDateTime())){
            String strDateFormat = "yyyy-MM-dd";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            smartParkAttendanceStatisticsQO.setDateTime(sdf.format(date));
        }

        try {
            BeanUtil.conversion(smartParkAttendanceStatisticsQO,smartParkAttendanceStatistics);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (smartParkAttendanceStatisticsQO.getNeedPage()) {
            smartParkAttendanceStatistics.setOrderByClause("clock_in_date desc limit " + (smartParkAttendanceStatisticsQO.getPageIndex() - 1) * smartParkAttendanceStatisticsQO.getPageSize() + "," + smartParkAttendanceStatisticsQO.getPageSize());
        } else {
            smartParkAttendanceStatistics.setOrderByClause("clock_in_date asc");
        }
        List<SmartParkAttendanceStatistics> list = smartParkAttendanceStatisticsMapper.selectByDay(smartParkAttendanceStatistics);
        List<SmartParkAttendanceStatistics> smartParkAttendanceStatisticsList = new ArrayList<>();
        for (SmartParkAttendanceStatistics statistics:list){
            if(Integer.valueOf(statistics.getClockInStatusName()) == 0){
                statistics.setClockInStatusName("正常");
            }else {
                statistics.setClockInStatusName("异常");
            }
            smartParkAttendanceStatisticsList.add(statistics);
        }

        long count = smartParkAttendanceStatisticsMapper.countByDay(smartParkAttendanceStatistics);

        return ResponseBodyWrapperUtil
                .successListWrapper(smartParkAttendanceStatisticsList, count, smartParkAttendanceStatisticsQO, SmartParkAttendanceStatistics.class);
    }

    /**
     * 统计每月每天是否有异常数据
     * @Description:  统计每月每天是否有异常数据
     * @Author: ChenYong
     * @Date: 2021/9/2 0002
     * @param smartParkAttendanceStatistics:
     * @Return: CommonResponseBody<List<SmartParkAttendanceStatistics>>
     */
    @Override
    public CommonResponseBody<List<SmartParkAttendanceStatistics>> selectByMonthAbnormal(SmartParkAttendanceStatistics smartParkAttendanceStatistics) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(StringUtils.isEmpty(smartParkAttendanceStatistics.getStartTime()) && StringUtils.isEmpty(smartParkAttendanceStatistics.getEndTime())){
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.set(Calendar.DAY_OF_MONTH,1);

            Calendar endCalendar = Calendar.getInstance();
            endCalendar.set(Calendar.DAY_OF_MONTH, 31);

            simpleDateFormat.format(endCalendar.getTime());
            smartParkAttendanceStatistics.setStartTime(simpleDateFormat.format(startCalendar.getTime()));
            smartParkAttendanceStatistics.setEndTime(simpleDateFormat.format(endCalendar.getTime()));
        }

        List<SmartParkAttendanceStatistics> list = smartParkAttendanceStatisticsMapper.selectByMonthAbnormal(smartParkAttendanceStatistics);
        List<SmartParkAttendanceStatistics> smartParkAttendanceStatisticsList = new ArrayList<>();
        for (SmartParkAttendanceStatistics statistics:list){
            if(Integer.valueOf(statistics.getClockInStatusName()) == 0){
                statistics.setClockInStatusName("true");
            }else {
                statistics.setClockInStatusName("false");
            }
            smartParkAttendanceStatisticsList.add(statistics);
        }

        CommonResponseBody commonResponseBody = new CommonResponseBody();
        commonResponseBody.setData(smartParkAttendanceStatisticsList);
        commonResponseBody.setErrMsg("请求成功");
        commonResponseBody.setErrCode("200");
        commonResponseBody.setSuccess(true);
        return commonResponseBody;
    }
}
